﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace UFrame
{
    public class UIAnimaPosition : MonoBehaviour
    {
        public AnimationCurve curve;
        public Transform targetPoint;
        public Transform originPoint;
        private Transform target;
        public float time = 0.5f;
        private float speed = 10f;

        private void Start()
        {
            if (originPoint == null)
                originPoint = transform;
        }

        private void Update()
        {
            if (target == null)
            {
                target = targetPoint;
                CalTime();
            }
            
            transform.position += (target.position - transform.position).normalized * speed * Time.deltaTime;
            var dis = Vector3.Distance(transform.position, target.position);
            if (dis <= 10)
            {
                if (target == targetPoint)
                {
                    target = originPoint;
                }
                else
                {
                    target = targetPoint;
                }

                CalTime();
            }
        }

        public void CalTime()
        {
            var dis = Vector3.Distance(transform.position, target.position);
            speed = dis / time;
        }
    }
}